package plp.functional2.expression;

import java.util.HashMap;

import plp.expressions1.util.Tipo;
import plp.expressions2.expression.Expressao;
import plp.expressions2.expression.Id;
import plp.expressions2.expression.Valor;
import plp.expressions2.expression.ValorString;
import plp.expressions2.memory.AmbienteCompilacao;
import plp.expressions2.memory.AmbienteExecucao;
import plp.expressions2.memory.VariavelJaDeclaradaException;
import plp.expressions2.memory.VariavelNaoDeclaradaException;
import plp.functional1.expression.TuplaException;

public class ExpKeyWord implements Expressao {

	private Id id;

	private Expressao expressao2;
   
	private ValorString key;
	
	private Valor valor;

	public ExpKeyWord(Id id, Expressao exp2) {
		this.id = id;
		expressao2 = exp2;
	}

	public Valor avaliar(AmbienteExecucao amb)
			throws VariavelNaoDeclaradaException, VariavelJaDeclaradaException,
			TuplaException, MapException {
		try {
			key = new ValorString (id.toString());
			valor = (Valor)expressao2.avaliar(amb);
			HashMap<Expressao, Expressao> map = new HashMap<Expressao, Expressao>();
            map.put(key, valor);
            
            return new ValorMap(map);
            
		} catch (ClassCastException e) {
			throw new MapException("erro de tipo.");
		}
	}

	public boolean checaTipo(AmbienteCompilacao amb)
			throws VariavelNaoDeclaradaException, VariavelJaDeclaradaException {
		return true;
	}

	public Tipo getTipo(AmbienteCompilacao amb)
			throws VariavelNaoDeclaradaException, VariavelJaDeclaradaException {
		return valor.getTipo(amb);
	}

}
